package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CrmOrderDetails;
import com.ruoyi.crm.mapper.CrmOrdersMapper;
import com.ruoyi.crm.domain.CrmOrders;
import com.ruoyi.crm.service.ICrmOrdersService;

import javax.annotation.Resource;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-14
 */
@Service
public class CrmOrdersServiceImpl implements ICrmOrdersService 
{
    @Resource
    private CrmOrdersMapper crmOrdersMapper;

    /**
     * 查询订单信息
     * 
     * @param orderUuid 订单信息主键
     * @return 订单信息
     */
    @Override
    public CrmOrders selectCrmOrdersByOrderUuid(String orderUuid)
    {
        return crmOrdersMapper.selectCrmOrdersByOrderUuid(orderUuid);
    }

    /**
     * 查询订单信息列表
     * 
     * @param crmOrders 订单信息
     * @return 订单信息
     */
    @Override
    public List<CrmOrders> selectCrmOrdersList(CrmOrders crmOrders)
    {
        return crmOrdersMapper.selectCrmOrdersList(crmOrders);
    }

    /**
     * 新增订单信息
     * 
     * @param crmOrders 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCrmOrders(CrmOrders crmOrders)
    {
        crmOrders.setCreateBy( SecurityUtils.getUsername() );
        crmOrders.setCreateTime(DateUtils.getNowDate());
        int rows = crmOrdersMapper.insertCrmOrders(crmOrders);
        insertCrmOrderDetails(crmOrders);
        return rows;
    }

    /**
     * 修改订单信息
     * 
     * @param crmOrders 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCrmOrders(CrmOrders crmOrders)
    {
        crmOrders.setUpdateTime(DateUtils.getNowDate());
        crmOrders.setUpdateBy(SecurityUtils.getUsername());
        crmOrdersMapper.deleteCrmOrderDetailsByOrderUuid(crmOrders.getOrderUuid());
        insertCrmOrderDetails(crmOrders);
        return crmOrdersMapper.updateCrmOrders(crmOrders);
    }

    @Transactional
    @Override
    public int update(String[] array)
    {
        return crmOrdersMapper.update(array);
    }
    /**
     * 修改订单信息
     *
     * @param crmOrders 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int submitConfirm(CrmOrders crmOrders)
    {

        return crmOrdersMapper.submitConfirm(crmOrders);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderUuids 需要删除的订单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCrmOrdersByOrderUuids(String[] orderUuids)
    {
        crmOrdersMapper.deleteCrmOrderDetailsByOrderUuids(orderUuids);
        return crmOrdersMapper.deleteCrmOrdersByOrderUuids(orderUuids);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderUuid 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteCrmOrdersByOrderUuid(String orderUuid)
    {
        crmOrdersMapper.deleteCrmOrderDetailsByOrderUuid(orderUuid);
        return crmOrdersMapper.deleteCrmOrdersByOrderUuid(orderUuid);
    }

    /**
     * 新增订单明细信息
     * 
     * @param crmOrders 订单信息对象
     */
    public void insertCrmOrderDetails(CrmOrders crmOrders)
    {
        List<CrmOrderDetails> crmOrderDetailsList = crmOrders.getCrmOrderDetailsList();
        String orderUuid = crmOrders.getOrderUuid();
        if (StringUtils.isNotNull(crmOrderDetailsList))
        {
            List<CrmOrderDetails> list = new ArrayList<CrmOrderDetails>();
            for (CrmOrderDetails crmOrderDetails : crmOrderDetailsList)
            {
                crmOrderDetails.setOrderUuid(orderUuid);
                list.add(crmOrderDetails);
            }
            if (list.size() > 0)
            {
                crmOrdersMapper.batchCrmOrderDetails(list);
            }
        }
    }
}
